layui.use(['layer', 'form', 'table', 'ztree', 'laydate', 'admin', 'ax'], function () {
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ZTree = layui.ztree;
    var $ax = layui.ax;
    var laydate = layui.laydate;
    var admin = layui.admin;

    /**
     * 系统管理--用户管理
     */
    var MgrWalletConfig = {
        tableId: "userTable",    //表格id
        condition: {
            imei: "",
            type: "",
            timeLimit: ""
        }
    };

    /**
     * 初始化表格的列
     */
    MgrWalletConfig.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, sort: true, title: 'id'},
            {field: 'imei', sort: true, title: 'imei'},
            {field: 'wallet', sort: true, title: '钱包'},
            {field: 'type', sort: true, title: '类型'},
            {field: 'maxAmount', sort: true, title: '钱包额度'},
            {field: 'threshold', sort: true, title: '钱包预警额度'},
            {field: 'isMax', sort: true, title: '是否达到上限'},
            {field: 'enable', sort: true, templet: '#statusTpl', title: '状态'},
            {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 280}
        ]];
    };

    /**
     * 点击查询按钮
     */
    MgrWalletConfig.search = function () {
        var queryData = {};
        queryData['imei'] = MgrWalletConfig.condition.imei;
        queryData['type'] = $("#type").val();
        table.reload(MgrWalletConfig.tableId, {where: queryData});
    };

    /**
     * 弹出添加用户对话框
     */
    MgrWalletConfig.openAddWalletConfig = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '添加钱包',
            content: Feng.ctxPath + '/walletConfig/walletConfigadd',
            end: function () {
                admin.getTempData('formOk') && table.reload(MgrWalletConfig.tableId);
            }
        });
    };

    /**
     * 点击编辑用户按钮时
     *
     * @param data 点击按钮时候的行数据
     */
    MgrWalletConfig.onEditWalletConfig = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '编辑钱包',
            content: Feng.ctxPath + '/walletConfig/walletConfigedit?id=' + data.id,
            end: function () {
                admin.getTempData('formOk') && table.reload(MgrWalletConfig.tableId);
            }
        });
    };

    /**
     * 点击删除用户按钮
     *
     * @param data 点击按钮时候的行数据
     */
    MgrWalletConfig.onDeleteWalletConfig = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/WalletConfig/delete?id="+ data.id, function () {
                table.reload(MgrWalletConfig.tableId);
                Feng.success("删除成功!");
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("userId", data.userId);
            ajax.start();
        };
        Feng.confirm("是否删除用户" + data.account + "?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + MgrWalletConfig.tableId,
        url: Feng.ctxPath + '/walletConfig/list',
        page: true,
        height: "full-98",
        cellMinWidth: 100,
        cols: MgrWalletConfig.initColumn()
    });

    //渲染时间选择框
    laydate.render({
        elem: '#timeLimit',
        range: true,
        max: Feng.currentDate()
    });


    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        MgrWalletConfig.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        MgrWalletConfig.openAddWalletConfig();
    });


    // 工具条点击事件
    table.on('tool(' + MgrWalletConfig.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            MgrWalletConfig.onEditWalletConfig(data);
        } else if (layEvent === 'delete') {
            MgrWalletConfig.WalletConfig(data);
        }
    });

    // 修改user状态
    form.on('switch(enable)', function (obj) {

        var userId = obj.elem.value;
        var checked = obj.elem.checked ? true : false;

        MgrWalletConfig.changeUserStatus(userId, checked);
    });

});
